var app = getApp()
Component({
  properties: {
    scrollArr: {
      type: Array,
      value: [],
      observer: function () {

      }
    },
    flag: {
      type: Number,
      value: 0,
      observer: function () {

      }
    },
    isscroll: {
      type: Boolean,
      value: true,
      observer: function () {

      }
    },
    selectedKey: {
      type: Number,
      value: 1,
      observer: function (newValue, oldValue) {
        if (this.properties.isscroll && newValue !== oldValue && newValue >= 0 && newValue < this.properties.scrollArr.length) {
          let distance = newValue > 1 ? -(newValue - 1) * this.data.liHeight : (newValue === 1 ? 0 : this.data.liHeight)
          console.log('value: ', this.properties.isscroll, newValue, distance)
          this.animation.translate(0, distance + 'px').step()
          newValue = newValue
          this.setData({
            startCount: newValue,
            animationData: this.animation.export()
          })
        }
      }
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    liHeight: 35,
    animationData: null,
    startY: 0, // 起始触摸位置y坐标
    startCount: 1 //滚动最后选中的下表
  },
  created () {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
    })
    animation.translate(0, 0).step()
    this.animation = animation
    this.setData({
      animationData: animation.export()
    })
  },
  methods: {
    touchstart(e) {
      let touches = e.touches[0]
      this.setData({
        startY: touches.pageY
      })
    },
    touchmove(e) {
      let touches = e.touches[0]
      if (touches && touches.pageY) {
        this.touchposition = touches.pageY
      }
    },
    touchend(e) {
      
      console.log(this.data.startY, this.touchposition)
      if (this.touchposition != undefined && this.data.startY != undefined) {
        let diff = this.data.startY - this.touchposition //大于0是上拉 ，小于0是下拉
        let count = Math.floor(Math.abs(diff) / this.data.liHeight)
        let countkey = diff > 0 ? this.data.startCount + count : this.data.startCount - count
        if (countkey >= this.data.scrollArr.length) {
          count = (this.data.scrollArr.length - 1) - this.data.startCount
          countkey = this.data.scrollArr.length - 1
        }
        if (countkey < 0) {
          count = this.data.startCount
          countkey = 0
        }
        this.setData({
          startCount: countkey
        })
        
        this.touchevent()
        let distance = countkey > 1 ? -(countkey - 1) * this.data.liHeight : (countkey === 1 ? 0 : this.data.liHeight)

       // let distance = diff < 0 ? Math.abs((countkey - 1)) * this.data.liHeight : -(countkey - 1) * this.data.liHeight
        console.log('-----------', countkey, distance)
        this.animation.translate(0, distance + 'px').step()
        this.setData({
          animationData: this.animation.export()
        })
      }
    },
    touchevent () {
      var myEventDetail = { selectKey: this.data.startCount, flag: this.properties.flag} // detail对象，提供给事件监听函数
      var myEventOption = {} // 触发事件的选项
      // console.log('fffffff')
      this.triggerEvent('touchevent', myEventDetail, myEventOption)
    }
  }
})